package com.whub507.msgmanager.controller;


import com.whub507.msgmanager.common.Field;
import com.whub507.msgmanager.common.ResResult;
import com.whub507.msgmanager.common.vo.AddGroupVO;
import com.whub507.msgmanager.common.vo.GroupVO;
import com.whub507.msgmanager.common.vo.UserVO;
import com.whub507.msgmanager.domain.Group;
import com.whub507.msgmanager.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Api(tags = "自定义对象组")
@RequestMapping("/group")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @GetMapping("/getGroup")
    @ApiOperation("获取该用户下的所有对象组")
    public ResResult<List<GroupVO>> getGroup(Integer userId){
        List<GroupVO> res = groupService.getGroup(userId);

        List<Field> fields = new ArrayList<>();
        fields.add(new Field("groupname","分组名字"));
        fields.add(new Field("name","成员名字"));
        fields.add(new Field("phone","成员电话"));

        return ResResult.ok(res, fields, "返回该用户下所有对象组");
    }

    @PostMapping("/addGroup")
    @ApiOperation("新建对象组")
    public ResResult<Boolean> addGroup(@RequestBody AddGroupVO addGroupVO){
        if (!groupService.addGroup(addGroupVO)){
            return ResResult.error(507, "该成员已存在同名对象组，请勿重复添加！");
        }
        return ResResult.ok(Boolean.TRUE, "新建成功");
    }

    @GetMapping("/getUsers")
    @ApiOperation("获取现存用户体系结构")
    public ResResult<List<UserVO>> getUsers(){
        List<UserVO> res = groupService.getUserStructure();

        List<Field> fields = new ArrayList<>();
        fields.add(new Field("department","所属部门"));
        fields.add(new Field("username","用户名"));
        fields.add(new Field("phone","用户电话"));

        return ResResult.ok(res, fields, "返回整个体系结构");
    }

}
